layui.use(['form', 'util'], function () {
    //获取iframe的body元素
    var form = layui.form;
    var util = layui.util
    var $ = layui.$
    //处理属性 为 lay-active 的所有元素事件
    // util.event('lay-active', {
    //     grade: function (othis) {
    //         var data = othis[0].dataset
    //         $("#uid").val(data.id);
    //         var ind = layer.open({
    //             //基本层类型
    //             type: 1,
    //             shadeClose: false,
    //             scrollbar: false,
    //             area: ['338px', '190px'],
    //             //标题
    //             title: data.name + data.year + "年满意度调查",
    //             //内容
    //             content: $('#commentForm'),
    //             //按钮
    //             btn: ['确定', '取消'],
    //             //确定按钮
    //             yes: () => {
    //                 if (validateCode()) {
    //                     //开启loading
    //                     const loading = layer.load(2);
    //                     //获取表单内的所有值
    //                     var formObj = form.val('scoreForm');
    //                     $.ajax({
    //                         url: '/grade/grade',
    //                         method: "post",
    //                         contentType: 'application/json;charset=utf-8',
    //                         data: JSON.stringify(formObj),
    //                         dataType: "json",
    //                         success: function (data) {
    //                             layer.close(loading);
    //                             if (data.code == 200) {
    //                                 layer.msg("评价成功！", {icon: 6, time: 1000});
    //                                 setTimeout(function () {
    //                                     window.location.href = getRootPath() + '/userGrade'
    //                                 }, 1100);
    //                                 layer.close(ind);
    //                             } else {
    //                                 layer.alert(data.msg, {icon: 5, time: 1500});
    //                                 layer.close(ind);
    //                             }
    //                         },
    //                         error: function () {
    //                             layer.alert('连接服务器失败...', {icon: 5, time: 1500});
    //                         }
    //                     });
    //                 }
    //             },
    //         });
    //     }
    // });
    util.event('lay-active', {
        grade: function (othis) {
            var data = othis[0].dataset
            $("#uid").val(data.id);
            var ind = layer.open({
                //基本层类型
                type: 1,
                shadeClose: false,
                scrollbar: false,
                area: ['613px', '310px'],
                //标题
                title: data.name + data.year + "年满意度调查",
                //内容
                content: $('#commentForm'),
                //按钮
                btn: ['确定', '取消'],
                //确定按钮
                yes: () => {
                    if (validateCode()) {
                        //开启loading
                        const loading = layer.load(2);
                        //获取表单内的所有值
                        var formObj = form.val('scoreForm');
                        $.ajax({
                            url: '/grade/grade',
                            method: "post",
                            contentType: 'application/json;charset=utf-8',
                            data: JSON.stringify(formObj),
                            dataType: "json",
                            success: function (data) {
                                layer.close(loading);
                                if (data.code == 200) {
                                    layer.msg("评价成功！", {icon: 6, time: 1000});
                                    setTimeout(function () {
                                        window.location.href = getRootPath() + '/userGrade'
                                    }, 1100);
                                    layer.close(ind);
                                } else {
                                    layer.alert(data.msg, {icon: 5, time: 1500});
                                    layer.close(ind);
                                }
                            },
                            error: function () {
                                layer.alert('连接服务器失败...', {icon: 5, time: 1500});
                            }
                        });
                    }
                },
            });
        },
        sub: function (othis) {
            //获取表单内的所有值
            var formObj = form.val('form');
            const loading = layer.load(2);
            if (validate_all()){
                //发送数据到接口
                $.ajax({
                    url: '/grade/gradeAll',
                    method: "post",
                    contentType: 'application/json;charset=utf-8',
                    data: JSON.stringify(formObj),
                    dataType: "json",
                    success: function (data) {
                        layer.close(loading);
                        if (data.code == 200) {
                            layer.msg("评价成功！", {icon: 6, time: 1000});
                            setTimeout(function () {
                                window.location.href = getRootPath() + '/userGrade'
                            }, 1100);
                        } else {
                            layer.msg(data.msg, {icon: 5, time: 2000});
                        }
                    }
                });
            } else {
                layer.close(loading);
                layer.alert("您还有未选择项，请选择，谢谢~");
            }
        }
    });


});

function validateCode() {
    if ($('input:radio[name="score"]:checked').val() == null) {
        layer.msg("请选择满意度！", {icon: 5, time: 1500});
        return false;
    }
    return true;
}

function getRootPath() {
    // 获取当前网址，如：http://localhost:8080/MyWeb/index.html
    var curWwwPath = window.document.location.href;
    // 获取主机地址之后的目录，如： MyWeb/index.html
    var pathName = window.document.location.pathname;
    var pos = curWwwPath.indexOf(pathName);
    // 获取主机地址，如： http://localhost:8080
    var localhostPath = curWwwPath.substring(0, pos);
    return localhostPath;
}

function validate_all() {
    var radioName = new Array();
    $(":radio").each(function () {
        if ($(this).attr("name") != 'score') {
            radioName.push($(this).attr("name"));
        }
    });
    radioName.sort();
    $.unique(radioName);
    for (var i = 0;i < radioName.length;i++){
        if (!checkRadio(radioName[i])) {
            return false;
        }
    }
    return true;
}

function checkRadio(radioName) {
    return $("input[name=" + radioName + "]:checked").val() == null ? false : true;
}


